﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Display.CollectionsUI {
    import flash.events.*;
    import Engine.Managers.*;
    import flash.display.*;
    import flash.geom.*;
    import flash.utils.*;
    import Display.DialogUI.*;
    import org.aswing.*;
    import Classes.*;
    import Classes.util.*;
    import com.greensock.*;
    import Display.*;
    import Display.aswingui.*;
    import Display.hud.*;
    import com.greensock.easing.*;

    public class CollectionPanelPopout extends GenericDialog {

        protected static const OFFSET_X:int = 130;
        protected static const OFFSET_Y:int = 90;
        public static const MAX_SLOTS:int = 5;
        public static const SLOT_SIDE_LENGTH:int = 54;
        public static const GRID_MARGIN:int = 5;
        public static const TOTAL_WIDTH:int = 280;
        public static const NAME_HEIGHT:int = 25;
        public static const DISAPPEAR_TIMEOUT:int = 2;

        protected static var m_instance:CollectionPanelPopout;

        protected var m_itemCount:int = 0;
        protected var m_currentCollection:Collection;
        protected var m_isOpen:Boolean;
        protected var m_mouseIsOver:Boolean;
        protected var m_waitingForMouseOut:Boolean = false;
        protected var m_shownTime:Number;
        protected var m_isActive:Boolean;
        protected var m_completedCollectionsShown:Array;
        protected var m_fadeInCellLoaded:Boolean = false;

        public function CollectionPanelPopout(_arg1:Function=null){
            super("", "", 0, _arg1, "", "", false);
            m_centered = false;
            this.m_completedCollectionsShown = new Array();
        }
        public static function getInstance():CollectionPanelPopout{
            if (!m_instance){
                m_instance = new (CollectionPanelPopout)();
            };
            return (m_instance);
        }

        override protected function init():void{
            m_holder = new Sprite();
            this.addChild(m_holder);
            m_jwindow = new JWindow(m_holder);
            m_content = m_holder;
            m_content.addEventListener("close", closeMe, false, 0, true);
            this.x = this.getOffstageX();
            this.y = this.getStageY();
            this.m_isOpen = false;
            this.m_isActive = false;
        }
        override protected function doTrackDialogActions():Boolean{
            return (false);
        }
        override protected function loadAssets():void{
            Global.delayedAssets.get(DelayedAssetLoader.INVENTORY_ASSETS, makeAssets);
        }
        override protected function onAssetsLoaded(_arg1:Event=null):void{
            var _local2:Dictionary = new Dictionary();
            _local2["collectionFlyout"] = (new m_comObject.collectionFlyout() as DisplayObject);
            m_jpanel = this.createDialogView(_local2);
            finalizeAndShow();
            ASwingHelper.prepare(m_jpanel);
            ASwingHelper.prepare(m_jwindow);
            this.buttonMode = true;
            this.mouseChildren = false;
            Global.hud.addChild(this);
            this.createEventListener();
            Global.stage.addEventListener(FullScreenEvent.FULL_SCREEN, this.onFullScreen);
        }
        private function onClick(_arg1:Event):void{
            if (!Global.guide.isActive()){
                UI.displayCollections(this.m_currentCollection.name);
            };
        }
        public function onUpdate(_arg1:Number):void{
            if (((((!(this.m_isActive)) || (!(this.m_isOpen)))) || (UI.isModalDialogOpen))){
                return;
            };
            if (this.m_shownTime >= DISAPPEAR_TIMEOUT){
                if (this.m_fadeInCellLoaded){
                    this.stopAnim();
                    this.m_isActive = false;
                };
            };
            this.m_shownTime = (this.m_shownTime + _arg1);
        }
        protected function restartDisappearTimer():void{
            this.m_shownTime = 0;
            this.m_isActive = true;
        }
        public function addItemByName(_arg1:String, _arg2:Point, _arg3:Point):Boolean{
            var _local6:Array;
            var _local7:int;
            var _local4:Item = Global.gameSettings.getItemByName(_arg1);
            var _local5:Collection = Global.gameSettings.getCollectionByCollectableName(_local4.name);
            if (_local5){
                this.m_itemCount++;
                this.restartDisappearTimer();
                this.openPopout(_local5);
                _local6 = _local5.getCollectableNames();
                _local7 = 0;
                while (_local7 < _local6.length) {
                    if (_local6[_local7] == _arg1){
                        if (_arg2){
                            _arg2.x = ((this.getOnstageX() + GRID_MARGIN) + ((SLOT_SIDE_LENGTH + 2) * _local7));
                            _arg2.y = this.getStageY();
                        };
                        if (_arg3){
                            _arg3.x = SLOT_SIDE_LENGTH;
                            _arg3.y = (SLOT_SIDE_LENGTH + 4);
                        };
                        return (true);
                    };
                    _local7++;
                };
            };
            return (false);
        }
        protected function openPopout(_arg1:Collection):void{
            if (((((!(this.m_isOpen)) && (hasLoaded()))) && (_arg1))){
                this.rebuildCells(_arg1);
                this.startAnim();
            };
        }
        protected function rebuildCells(_arg1:Collection):void{
            this.m_currentCollection = _arg1;
            if (this.m_currentCollection){
                (m_jpanel as CollectionPanelPopoutView).prepopulatePanelSlots(this.m_currentCollection);
                (m_jpanel as CollectionPanelPopoutView).invalidateData();
            };
        }
        public function itemAppear(_arg1:String):void{
            this.verifyCollection(_arg1);
            var _local2:Item = Global.gameSettings.getItemByName(_arg1);
            var _local3:Collection = Global.gameSettings.getCollectionByCollectableName(_local2.name);
            (m_jpanel as CollectionPanelPopoutView).invalidateCell(_local2.name, _local3);
            (m_jpanel as CollectionPanelPopoutView).playFadeIn(_local2.name, _local3, this.onFadeInComplete, this.onFadeInCellLoaded);
        }
        public function verifyCollection(_arg1:String):void{
            var _local2:Item = Global.gameSettings.getItemByName(_arg1);
            var _local3:Collection = Global.gameSettings.getCollectionByCollectableName(_local2.name);
            this.restartDisappearTimer();
            this.openPopout(_local3);
            if ((((this.m_currentCollection == null)) || (!((_local3.name == this.m_currentCollection.name))))){
                this.rebuildCells(_local3);
            };
        }
        protected function onFadeInComplete():void{
            var _local1:GenericDialog;
            this.m_itemCount--;
            if (((this.m_currentCollection.isReadyToTradeIn()) && ((this.m_completedCollectionsShown.indexOf(this.m_currentCollection.name) == -1)))){
                this.m_completedCollectionsShown.push(this.m_currentCollection.name);
                if (Collection.completedEarlier(this.m_currentCollection.name)){
                    Sounds.play("collectionComplete");
                } else {
                    _local1 = new GenericDialog(ZLoc.t("Collections", "completed_collection", {collection_name:this.m_currentCollection.localizedName}));
                    UI.displayPopup(_local1);
                    Sounds.play("collectionComplete");
                    Collection.setCompletedEarlier(this.m_currentCollection.name, true);
                };
            };
        }
        protected function getOffstageX():Number{
            return (Global.stage.stageWidth);
        }
        protected function getOnstageX():Number{
            var _local1:Number = Global.hud.getComponent(HUD.COMP_XP).x;
            return (((_local1 - TOTAL_WIDTH) + OFFSET_X));
        }
        protected function getStageY():Number{
            var _local1:Number = Global.hud.getComponent(HUD.COMP_XP).y;
            return ((_local1 + OFFSET_Y));
        }
        protected function startAnim():void{
            this.m_isOpen = true;
            this.x = this.getOffstageX();
            this.y = this.getStageY();
            show();
            TweenLite.to(this, 0.6, {
                x:this.getOnstageX(),
                ease:Back.easeOut
            });
        }
        protected function stopAnim():void{
            if (this.m_mouseIsOver){
                this.m_waitingForMouseOut = true;
            } else {
                this.tweenBack();
            };
        }
        override protected function onShow():void{
            if (m_shown == false){
                if (m_modal){
                    InputManager.disableInput();
                    Global.ui.mouseEnabled = false;
                    Global.ui.mouseChildren = false;
                };
                if (m_lightbox){
                    Global.ui.displayLightbox(true);
                };
                if (useSystemCursor()){
                    m_cursorId = UI.setCursor(null);
                };
                this.mouseEnabled = (this.mouseChildren = true);
                m_shown = true;
            } else {
                m_showOnLoad = true;
            };
            if (!Global.guide.isActive()){
                Sounds.play("dialogSpawn");
            };
        }
        override protected function onHide():void{
            if (m_shown == true){
                if (m_modal){
                    InputManager.enableInput();
                    Global.ui.mouseEnabled = true;
                    Global.ui.mouseChildren = true;
                };
                if (m_lightbox){
                    Global.ui.displayLightbox(false);
                };
                m_shown = false;
                this.mouseEnabled = (this.mouseChildren = false);
                if (useSystemCursor()){
                    UI.removeCursor(m_cursorId);
                };
            };
            m_showOnLoad = false;
        }
        override protected function onHideComplete():void{
            super.onHideComplete();
            this.m_isOpen = false;
            if (this.m_itemCount > 0){
                this.openPopout(this.m_currentCollection);
            };
        }
        private function tweenBack():void{
            TweenLite.to(this, 0.4, {
                x:this.getOffstageX(),
                ease:Back.easeIn,
                onComplete:function ():void{
                    hide();
                }
            });
        }
        protected function onMouseOver(_arg1:MouseEvent):void{
            this.m_mouseIsOver = true;
        }
        protected function onMouseOut(_arg1:MouseEvent):void{
            this.m_mouseIsOver = false;
            if (this.m_waitingForMouseOut){
                this.m_waitingForMouseOut = false;
                this.tweenBack();
            };
        }
        override protected function hideTween(_arg1:Function):void{
            this.mouseEnabled = (this.mouseChildren = false);
            _arg1();
            Sounds.play("dialogClose");
        }
        override protected function showTween():void{
            this.mouseEnabled = (this.mouseChildren = false);
            var _local1:Point = new Point(m_content.width, m_content.height);
            var _local2:Matrix = new Matrix();
            _local2.translate((-(_local1.x) / 2), (-(_local1.y) / 2));
            _local2.scale(1, 1);
            _local2.translate((_local1.x / 2), (_local1.y / 2));
            m_content.transform.matrix = _local2;
            if (m_centered){
                centerPopup();
            };
            onShowComplete();
        }
        override protected function onFullScreen(_arg1:FullScreenEvent):void{
            if (((this.m_isActive) && (this.m_isOpen))){
                this.x = this.getOnstageX();
            } else {
                this.x = this.getOffstageX();
            };
            this.y = this.getStageY();
        }
        public function onFadeInCellLoaded(_arg1:Boolean):void{
            this.m_fadeInCellLoaded = _arg1;
        }
        override protected function createDialogView(_arg1:Dictionary):GenericDialogView{
            return (new CollectionPanelPopoutView(_arg1, m_message, m_title, m_type, m_callback));
        }
        protected function createEventListener():Boolean{
            this.addEventListener(MouseEvent.CLICK, this.onClick);
            this.addEventListener(MouseEvent.MOUSE_OVER, this.onMouseOver);
            this.addEventListener(MouseEvent.MOUSE_OUT, this.onMouseOut);
            return (true);
        }

    }
}//package Display.CollectionsUI 
